﻿#ifndef DRAWVIEW_H
#define DRAWVIEW_H
#include <QGraphicsView>

#include "rulebar.h"
#include "drawobj.h"

class QMouseEvent;
class GraphicsRectItem;

class DrawView : public QGraphicsView
{
    Q_OBJECT
public:
    DrawView(QGraphicsScene *scene);
    void zoomIn();
    void zoomOut();
    void newFile();
    bool loadFile(const QString &fileName);
    bool save();
    bool saveAs();
    bool saveFile(const QString &fileName);
    QString userFriendlyCurrentFile();
    QString currentFile() {return curFile;}
    void setModified( bool value ) {modified = value;}
    bool isModified() const {return modified;}
    void showContextMenu(GraphicsRectItem* item, const QPoint &pos);
    void showProperties(GraphicsRectItem *item, const QString &string);
    bool isPanning() const {return panning;}

signals:
    void positionChanged(int x , int y );
    void signalMenuCopy();
    void signalMenuPaste();

protected:
    void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;
    void mousePressEvent(QMouseEvent *event) override;
    void mouseMoveEvent(QMouseEvent * event) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QMouseEvent *event) override;
    void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
    void scrollContentsBy(int dx, int dy) Q_DECL_OVERRIDE;
    //拖动
    void dragEnterEvent(QDragEnterEvent* event) override;
    void dropEvent(QDropEvent* event) override;

    void updateRuler();
    QtRuleBar *m_hruler;
    QtRuleBar *m_vruler;
    QtCornerBox * box;

private:
    bool maybeSave();
    void setCurrentFile(const QString &fileName);
    QString strippedName(const QString &fullFileName);
    void loadCanvas();
    void loadConnectLines();
    GraphicsRectItem* getRectItemFormUnidId(QString unitId);

    void updateGlobalUnits();
    void updateGlobaPinMap();
    void writeUnitsToXml(QXmlStreamWriter &xml, const QString &basePath);
    void writeUnitElement(QXmlStreamWriter &xml, const Unit& unit, const QString &basePath);
    void writeMapConnections(QXmlStreamWriter &xml);
    void writeConnectionElement(QXmlStreamWriter &xml, const Pin& fromPin, const Pin& toPin);

    QString curFile;
    bool isUntitled;
    bool modified;

    //ctrl+右键拖动
    bool panning = false;
    QPoint lastPanPos;
};

#endif // DRAWVIEW_H
